package com.wanfajie.smsdistribute.ui.phone_group_content

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.wanfajie.smsdistribute.R

class EditDialog(
        ctx: Context,
        title: String,
        fieldName: String? = null,
        val onTextCommit: (String) -> Unit
): AlertDialog(ctx) {

    private val mView = View.inflate(ctx, R.layout.dialog_edit_field, null)
    private val mEdit: EditText

    init {
        setView(mView)
        val fieldNameView = mView.findViewById<TextView>(R.id.field_name)
        if (fieldName == null) {
            fieldNameView.visibility = View.GONE
        } else {
            fieldNameView.text = fieldName
        }

        mEdit = mView.findViewById(R.id.field_value)
        setTitle(title)
        setButton(DialogInterface.BUTTON_POSITIVE, ctx.getString(R.string.ok)) {
            dialog: DialogInterface, which: Int ->

            onTextCommit.invoke(getText())
        }

        setButton(
                DialogInterface.BUTTON_NEGATIVE,
                ctx.getString(R.string.cancel)
        ) { dialog, witch ->
        }
    }

    fun getText(): String {
        return mEdit.text.toString()
    }

    fun setText(v: String): EditDialog {
        mEdit.setText(v)
        return this
    }

    fun setInputType(type: Int): EditDialog {
        mEdit.inputType = type
        return this
    }
}
